<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Category_Model extends CI_Model
{

	function get_featured_product(){

		//$this->db->where('product_id', $product_id);
		$this->db->limit(12);
		$this->db->where('status',1);
		$this->db->order_by('product_id DESC');

		$query = $this->db->get('product');
		if ($query->num_rows() > 0 ) return $query->result();
		return array();		

	}
	
	function select($product_seo_name){
        $query = $this->db->get_where('product', array('product_seo_name'=>$product_seo_name));
		if($query ->num_rows() >0 ) {
			foreach($query ->result() as $row){
				$data[] = $row;
			}
			return $data;
		}
    }

	function get_list_product($category_id='', $page='', $limit=''){
		$query = $this->db->query('select * from product where category_id in 
									(select category_id from product_category where parent in 
										(select category_id from product_category where category_id = "'.$category_id.'") 
										or category_id = "'.$category_id.'") order by date_add desc');
		if(!empty($limit))
		$query = $this->db->query('select * from product where category_id in 
									(select category_id from product_category where parent in 
										(select category_id from product_category where category_id = "'.$category_id.'") 
										or category_id = "'.$category_id.'") order by date_add desc limit '.$page.','.$limit.'');

		//echo $this->db->last_query();

		if ($query->num_rows() > 0) return $query->result();
		return array();
	}
	
	function get_category_parent($child){

		return $this->db->query('select * from product_category where category_id in(select parent from product_category where category_id = "'.$child.'" )')->row();
	}
	
	function get_category_child($category_id){
		$this->db->where('category_id',$category_id);
		$query = $this->db->get('product_category');
		return $query->row();
	}
	
	function get_category($category_id){
		$this->db->where('category_id',$category_id);
		$query = $this->db->get('product_category');
		return $query->row();
	}
	
	function get_store(){
		$query = $this->db->get('store');
		if ($query->num_rows() > 0) return $query->result();
		return array();	
	}
	
	function get_list_product_low($category_id='', $page='', $limit=''){
		$query = $this->db->query('select * from product where category_id in 
									(select category_id from product_category where parent in 
										(select category_id from product_category where category_id = "'.$category_id.'") 
										or category_id = "'.$category_id.'") order by price asc');
		if(!empty($limit))
		$query = $this->db->query('select * from product where category_id in 
									(select category_id from product_category where parent in 
										(select category_id from product_category where category_id = "'.$category_id.'") 
										or category_id = "'.$category_id.'") order by price asc limit '.$page.','.$limit.'');

		//echo $this->db->last_query();

		if ($query->num_rows() > 0) return $query->result();
		return array();
	}
	
	function get_list_product_high($category_id='', $page='', $limit=''){
		$query = $this->db->query('select * from product where category_id in 
									(select category_id from product_category where parent in 
										(select category_id from product_category where category_id = "'.$category_id.'") 
										or category_id = "'.$category_id.'") order by price desc');
		if(!empty($limit))
		$query = $this->db->query('select * from product where category_id in 
									(select category_id from product_category where parent in 
										(select category_id from product_category where category_id = "'.$category_id.'") 
										or category_id = "'.$category_id.'") order by price desc limit '.$page.','.$limit.'');

		//echo $this->db->last_query();

		if ($query->num_rows() > 0) return $query->result();
		return array();
	}
	
}